package org.example.panel.userpanel;
import javax.swing.*;
import java.awt.event.*;
import org.example.model.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class ModifyPasswordPanel extends JPanel{
    public ModifyPasswordPanel(User user){
        //组件准备
        JTextField beforePassword = new JTextField(15);
        JTextField afterPassword = new JTextField(15);
        JButton modifyPassword = new JButton("修改密码");
        Box beforePasswordBox = Box.createHorizontalBox();
        Box afterPasswordBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        modifyPassword.addActionListener((ActionEvent e)->{
            if(beforePassword.getText().isEmpty()||afterPassword.getText().isEmpty()){
                JOptionPane.showMessageDialog(null,
                        "原密码或新密码不能为空", null, JOptionPane.WARNING_MESSAGE);
            }else if(beforePassword.getText().matches(".*\\s+.*")
                    ||afterPassword.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "原密码或新密码不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(UserOperation.isNotLogin(user.getUsername(), beforePassword.getText())){
                JOptionPane.showMessageDialog(null,
                        "原密码错误", null, JOptionPane.WARNING_MESSAGE);
            }else{
                UserOperation.addUser(user.getUsername(),afterPassword.getText());
                JOptionPane.showMessageDialog(null,
                        "修改密码成功", null, JOptionPane.WARNING_MESSAGE);
            }
        });
        //添加组件
        beforePasswordBox.add(new JLabel("原密码： "));
        beforePasswordBox.add(beforePassword);
        afterPasswordBox.add(new JLabel("新密码： "));
        afterPasswordBox.add(afterPassword);
        box.add(beforePasswordBox);
        box.add(Box.createVerticalStrut(5));
        box.add(afterPasswordBox);
        box.add(Box.createVerticalStrut(5));
        box.add(modifyPassword);
        add(box);
    }
}
